package com.ruoyi.web.controller.system.email;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysEmail;
import com.ruoyi.system.domain.SysEmailConfig;
import com.ruoyi.system.service.ISysEmailConfigService;
import com.ruoyi.system.service.ISysEmailService;
import com.ruoyi.web.controller.util.MailUtils;

/**
 * 发邮件Controller
 * 
 * @author ruoyi
 * @date 2022-09-24
 */
@RestController
@RequestMapping("/system/email")
public class SysEmailController extends BaseController
{
    @Autowired
    private ISysEmailService sysEmailService;
    
    @Autowired
    private ISysEmailConfigService sysEmailConfigService;

    /**
     * 查询发邮件列表
     */
    @PreAuthorize("@ss.hasPermi('system:email:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysEmail sysEmail)
    {
        startPage();
        List<SysEmail> list = sysEmailService.selectSysEmailList(sysEmail);
        return getDataTable(list);
    }

    /**
     * 导出发邮件列表
     */
    @PreAuthorize("@ss.hasPermi('system:email:export')")
    @Log(title = "发邮件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysEmail sysEmail)
    {
        List<SysEmail> list = sysEmailService.selectSysEmailList(sysEmail);
        ExcelUtil<SysEmail> util = new ExcelUtil<SysEmail>(SysEmail.class);
        util.exportExcel(response, list, "发邮件数据");
    }

    /**
     * 获取发邮件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:email:query')")
    @GetMapping(value = "/{elId}")
    public AjaxResult getInfo(@PathVariable("elId") Long elId)
    {
        return AjaxResult.success(sysEmailService.selectSysEmailByElId(elId));
    }

    /**
     * 新增发邮件
     */
    @PreAuthorize("@ss.hasPermi('system:email:add')")
    @Log(title = "发邮件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysEmail sysEmail)
    {
    	
    	SysEmailConfig sysEmailConfig=new SysEmailConfig();
    	SysEmail SysEmail=sysEmail;
    	//查询状态为正常的邮箱
    	sysEmailConfig.setState("0");
    	//获取当前时间
    	Date date = new Date();
    	SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	System.out.println(dateFormat.format(date));
    	//查询邮箱
    	List<SysEmailConfig> list = sysEmailConfigService.selectSysEmailConfigList(sysEmailConfig);
//    	System.out.println(list);
    	for(SysEmailConfig Email:list) {
    		if(SysEmail.getElSend().equals(Email.getAccount())) {
    			MailUtils mailUtils=new MailUtils();
    	    	boolean b=mailUtils.sendMail(SysEmail.getElRecive(), SysEmail.getElMessage(), SysEmail.getElSubject(), Email.getAccount(), Email.getPwd());
    	    	if(b) {
    	    		System.out.println("发送成功");
    	    		SysEmail.setElSendTime(""+dateFormat.format(date));
    	    		SysEmail.setElState("0");
    	    	}else {
    	    		SysEmail.setElSendTime(""+dateFormat.format(date));
    	    		SysEmail.setElState("1");
    	    		System.out.println("发送失败");
    	    	}
    		}else {
        		SysEmail.setElSendTime(""+dateFormat.format(date));
        		SysEmail.setElState("1");
    		}

    	}  	
        return toAjax(sysEmailService.insertSysEmail(sysEmail));
    }

    /**
     * 修改发邮件
     */
    @PreAuthorize("@ss.hasPermi('system:email:edit')")
    @Log(title = "发邮件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysEmail sysEmail)
    {
        return toAjax(sysEmailService.updateSysEmail(sysEmail));
    }

    /**
     * 删除发邮件
     */
    @PreAuthorize("@ss.hasPermi('system:email:remove')")
    @Log(title = "发邮件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{elIds}")
    public AjaxResult remove(@PathVariable Long[] elIds)
    {
        return toAjax(sysEmailService.deleteSysEmailByElIds(elIds));
    }
}
